package hivi.vn.hivigold.utils;

import hivi.vn.hivigold.R;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.text.DecimalFormat;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;

import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.util.DisplayMetrics;

public class Formats {
	public static String ConvertDateFromDb(Context c, String date)
	{
		date = date.substring(0, 10);
		String[] str = date.split("-"); 
		date = str[2] +"/"+ str[1] +"/"+ str[0] ;
		return date;
	}
	public static String DateFormatDisplay(Object date)
	{
		SimpleDateFormat df = new SimpleDateFormat("dd/MM/yyyy");
		return df.format(date);
	}
	public static String DateFormatInsert(Object date)
	{
		SimpleDateFormat df = new SimpleDateFormat("yyyyMMdd");
		return df.format(date);
	}
	public static String DateNow()
	{
		Calendar c = Calendar.getInstance();
		SimpleDateFormat df = new SimpleDateFormat("yyyyMMdd");
		String formattedDate = df.format(c.getTime());
		return formattedDate;
	}
	public static Date StartDate()
	{
		Calendar c = Calendar.getInstance();
		c.set(1900, 1, 1);
		return c.getTime();
	}
	public static Date CurrentDate()
	{
		Calendar c = Calendar.getInstance();
		return c.getTime();
	}
	public static int getComDateNow(Date date,String comID) {
		SimpleDateFormat sdf = new SimpleDateFormat(comID);
		return Integer.parseInt(sdf.format(date));
	}
	public static Date createDate(int year,int month,int day)
	{
		Calendar c = Calendar.getInstance();
		c.set(year, month, day);
		return c.getTime();
	}
	public static String number(Double num)
	{
		DecimalFormat df = new DecimalFormat("###,###,###.###");
		return df.format(num);
	}
	public static String percent(Double num)
	{
		DecimalFormat df = new DecimalFormat("###,###,###.##");
		return df.format(num);
	}
	public static String money(Double num)
	{
		DecimalFormat df = new DecimalFormat("###,###,###,###");
		return df.format(num);
	}
	public static String money_round(Double num)
	{
		long round = Math.round(num/1000)*1000;
		DecimalFormat df = new DecimalFormat("###,###,###,###");
		return df.format(round);
	}
	public static Bitmap creatBitmap(String path)   {
		Bitmap bm=null;
	    BitmapFactory.Options bfOptions=new BitmapFactory.Options();
	    bfOptions.inDither=false;                     //Disable Dithering mode
	    bfOptions.inPurgeable=true;                   //Tell to gc that whether it needs free memory, the Bitmap can be cleared
	    bfOptions.inInputShareable=true;              //Which kind of reference will be used to recover the Bitmap data after being clear, when it will be used in the future
	    bfOptions.inTempStorage=new byte[32 * 1024]; 
	    File file=new File(path);
	    FileInputStream fs=null;
	    try {
	        fs = new FileInputStream(file);
	    } catch (FileNotFoundException e) {
	        //TODO do something intelligent
	        e.printStackTrace();
	    }

	    try {
	        if(fs!=null) bm=BitmapFactory.decodeFileDescriptor(fs.getFD(), null, bfOptions);
	    } catch (IOException e) {
	        //TODO do something intelligent
	        e.printStackTrace();
	    } finally{ 
	        if(fs!=null) {
	            try {
	                fs.close();
	            } catch (IOException e) {
	                // TODO Auto-generated catch block
	                e.printStackTrace();
	            }
	        }
	    }
	    return bm;
	}
	public static Bitmap creatBitmap(Context c, String path)   {
		Bitmap bm=null;
	    BitmapFactory.Options bfOptions=new BitmapFactory.Options();
	    bfOptions.inDither=false;                     //Disable Dithering mode
	    bfOptions.inPurgeable=true;                   //Tell to gc that whether it needs free memory, the Bitmap can be cleared
	    bfOptions.inInputShareable=true;              //Which kind of reference will be used to recover the Bitmap data after being clear, when it will be used in the future
	    bfOptions.inTempStorage=new byte[32 * 1024]; 
	    File file=new File(path);
	    FileInputStream fs=null;
	    try {
	        fs = new FileInputStream(file);
	    } catch (FileNotFoundException e) {
	        //TODO do something intelligent
	        e.printStackTrace();
	    }

	    try {
	        if(fs!=null) 
	        	{
	        	bm=BitmapFactory.decodeFileDescriptor(fs.getFD(), null, bfOptions);
	        	BitmapFactory.Options o = new BitmapFactory.Options();
	        	o.inTargetDensity = DisplayMetrics.DENSITY_DEFAULT;
	        	Bitmap bmp = BitmapFactory.decodeResource(c.getResources(), R.drawable.nophoto , o);
	        	Bitmap bm2 = Bitmap.createScaledBitmap(bm, bmp.getWidth(), bmp.getHeight(), false);
	        	bm = bm2;
	        	}
	        
	    } catch (IOException e) {
	        //TODO do something intelligent
	        e.printStackTrace();
	    } finally{ 
	        if(fs!=null) {
	            try {
	                fs.close();
	            } catch (IOException e) {
	                // TODO Auto-generated catch block
	                e.printStackTrace();
	            }
	        }
	    }
	    return bm;
	}
}
